import store from "@/store"
import config from "@/config/index.config.js";
function HTTP(obj, config) {

	let defaultConfig = {
		isRes: false,
		loading: false
	}

	config = { ...defaultConfig,
		...config
	}


	// 如果需要显示loading,mask防止点击穿透
	config.loading && uni.showLoading({
		title: '加载中',
		mask: true
	});

	return new Promise((resolve, reject) => {
		const TOKEN = uni.getStorageSync("token") || "";
		//const TOKEN = "2ddf5a78-4a46-4b59-96fc-b4035616dd28";
		let options = {
			url: "",
			method: "GET",
			data: {},
			dataType: "json",
			header: {
				'content-type': 'application/json',
				"X-requested-With": "XMLHttpRequest",
				"ZC-TOKEN":TOKEN
			},
			success: (res) => {
				if(res.header.token){
				 uni.setStorageSync("token",res.header.token)
				}				 
				
				uni.hideLoading();
				// 状态码为200
				if (res.statusCode == 200) {
					let data = res.data;
					//console.log(data.code);
					//自动校验用户是否登录过期
					if (data.code ==401) {
						store.dispatch("reLogin");
						return;
					}
					
					//自动校验用户是否认证
					if (data.code == "-101") {
						uni.clearStorageSync('token');
						uni.showToast({
							title: "token过期，请重新登录",
							icon: "none",
							duration: 200
						})
						setTimeout(function() {
							store.dispatch("reLogin");						
						}, 200);
						return;
						
					}
					

					//返回 { code:10000,msg:"消息",data:[] }
					if (config.isRes) {
						resolve(data)
					}
					// 返回 data:[]
					else {
						if (data.code == "200") {
							resolve(data || true)
						} else {
							if(data.message){
							uni.showToast({
								title: data.message,
								icon: "none",
								duration: 2000
							})
							//reject(data.msg);
							
							}
							resolve(data || false)
						}
					}
				} else {
					uni.showToast({
						title: data.message,
						icon: "none",
						duration: 2000
					})
					reject("HTTP:状态码异常！");
				}
			},
			fail: (err) => {
				uni.hideLoading();
				// uni.showToast({
				// 	title: "网络异常，请稍后再试!",
				// 	icon: "none",
				// })
				// reject("网络异常，请稍后再试!");
				
				console.log("网络异常，请稍后再试!")
			},
			complete: () => {}
		}

		options = { ...options,
			...obj
		};
		
		const OPENID = uni.getStorageSync("openId");
		if (OPENID) options["header"]["openId"] = OPENID;

		if (options.url && options.method) {
			wx.request(options);
		} else {
			wx.showToast({
				title: 'HTTP：缺失参数',
				icon: "none",
				duration: 2000
			})
		}
	})

}



export default {
	GET(url, data = {}, config) {
		return HTTP({
			url,
			data,
			method: "GET"
		}, config);
	},
	POST(url, data = {}, config) {
		return HTTP({
			url,
			data,
			method: "POST"
		}, config);
	},
	POSTurl(url, data = {}, config) {
		 let paramsData = "";
		for (var Key in data) {
			paramsData += '&' + Key + '=' + data[Key];	
		}
		var url = url +'?'+ paramsData.replace('&','',1);
		console.log(url);
		return HTTP({
			url,
			//data,
			method: "POST"
		}, config);
	},

	POSTformdata(url, data = {}, config) {
		const TOKEN = uni.getStorageSync("token") || "";
		return HTTP({
			url,
			data,
			method: "POST",
			header:{
				'content-type': 'application/x-www-form-urlencoded',
				"X-requested-With": "XMLHttpRequest",
				// "token":TOKEN
			}
		}, config);
	}
}
